package be.kuleuven.cs.mop.domain.model.impl;


import static org.junit.Assert.*;

import org.junit.*;

import be.kuleuven.cs.mop.TestEnvironment;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;



public class ResourceImplTest {
	
	private static TestEnvironment ENV;
	
	private static String			constructorDescription = "Constructor Test 1";
	private static ResourceImpl		resourceTest1, resourceTest2, resourceTest3;
	
	
	
	@Before
	public void setUp() throws Exception {
		ENV = TestEnvironment.newInstance();
		
		resourceTest1	= new ResourceImpl(ENV.world, "Test room",ENV.resourceTypes.get(0));
		resourceTest2	= new ResourceImpl(ENV.world, "Test tool",ENV.resourceTypes.get(1));
		resourceTest3	= null;
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}
	
	@After
	public void tearDown() throws Exception {}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {}
	
	@Test
	public void testConstructorLegalCase() throws TaskManagerException{
		resourceTest2 = new ResourceImpl(ENV.world, constructorDescription,ENV.resourceTypes.get(0));
		assertEquals(resourceTest2.getDescription(), constructorDescription);
		assertEquals(resourceTest2.getResourceType(), ENV.resourceTypes.get(0));
	}
	
	@Test(expected=NullPointerException.class)
	public void testConstructorIllegalCase1() throws TaskManagerException{
		new ResourceImpl(null, constructorDescription,ENV.resourceTypes.get(0));
	}
	
	@Test(expected=TaskManagerException.class)
	public void testConstructorIllegalCase2() throws TaskManagerException{
		new ResourceImpl(ENV.world, null,ENV.resourceTypes.get(0));
	}
	
	@Test(expected=TaskManagerException.class)
	public void testConstructorIllegalCase3() throws TaskManagerException{
		new ResourceImpl(ENV.world, "", null);
	}
	
	@Test
	public void testConstructorValid() throws TaskManagerException {
		new ResourceImpl(ENV.world, "valid resource", ENV.resourceTypes.get(0));
	}
	
	@Test
	public void testAddReservationLegalCase() throws TaskManagerException {
		assertTrue(resourceTest1.getReservations().isEmpty());
		resourceTest1.addReservation(ENV.task1, ENV.datum1, ENV.duration);
		assertFalse(resourceTest1.getReservations().isEmpty());
		
		final ReservationImpl reservation =
			resourceTest1.getReservations().iterator().next();
		
		assertEquals(reservation.getResource(),	resourceTest1);
		assertEquals(reservation.getTask(),		ENV.task1);
		assertEquals(reservation.getDate(),		ENV.datum1);
		assertEquals(reservation.getDuration(),	ENV.duration);
	}
	
	@Test(expected=TaskManagerException.class)
	public void testAddReservationIllegalCase1() throws TaskManagerException{
		resourceTest1.addReservation(ENV.task1, ENV.datum1, ENV.duration);
		resourceTest1.addReservation(ENV.task1, ENV.datum1, ENV.duration);
	}
	
	@Test(expected=TaskManagerException.class)
	public void testAddReservationIllegalCase2() throws TaskManagerException{
		resourceTest1.addReservation(null, null, 0);
	}
	
	@Test
	public void testCompareTo() throws TaskManagerException {
		resourceTest1.addReservation(ENV.task1, ENV.datum3, ENV.duration);
		final ResourceImpl resource = new ResourceImpl(ENV.world, "torture room",ENV.resourceTypes.get(0));
		assertTrue(resourceTest1.compareTo(resource) < 0);
	}
	
	@Test
	public void testRemoveReservationLegalCase() throws TaskManagerException{
		resourceTest3 = new ResourceImpl(ENV.world, "valid resource", ENV.resourceTypes.get(0));
		resourceTest3.addReservation(ENV.task1, ENV.datum1, ENV.duration);
		assertFalse(resourceTest3.getReservations().isEmpty());
		final ReservationImpl reservation =
			resourceTest3.getReservations().iterator().next();
		assertTrue(resourceTest3.getReservations().contains(reservation));
		resourceTest3.removeReservation(reservation);
		assertFalse(resourceTest3.getReservations().contains(reservation));
		assertTrue(resourceTest3.getReservations().isEmpty());
	}
	
	@Test(expected=NullPointerException.class)
	public void testRemoveReservationIllegalCase1() throws TaskManagerException{
		resourceTest1.removeReservation(null);
	}
	
	@Test(expected=TaskManagerException.class)
	public void testRemoveReservationIllegalCase2() throws TaskManagerException{
		resourceTest1.addReservation(ENV.task1, ENV.datum1, ENV.duration);
		assertFalse(resourceTest1.getReservations().isEmpty());
		final ReservationImpl reservation =
			resourceTest1.getReservations().iterator().next();
		resourceTest2.removeReservation(reservation);
	}
	
	@Test
	public void testValidation() {
		assertNotNull(resourceTest1.toString());
		assertFalse(resourceTest1.toString().isEmpty());
//		assertFalse(ResourceImpl.isValidDescription(null));
//		assertFalse(ResourceImpl.isValidDescription(""));
	}

}